﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WmsApp.Service;
using WmsApp.WmsService;

namespace WmsApp.Window
{
    public partial class FormInvoiceBillInStoreBillSelector : BaseForm
    {
        private string providerCode = string.Empty;
        private const string ConditionInStoreBill =
         " and ProviderCode ='{0}' and AmountTax> InStoreBillAmount and  (PayApplyBillID = '' or PayApplyBillID is null)";
        public FormInvoiceBillInStoreBillSelector(string providerCode,List<string> existInvioceBillNos)
        {
            InitializeComponent();
            FInvoiceBill = new List<InvoiceBill>();

            if(String.IsNullOrEmpty(providerCode))
            {
                this.CmdOk.Enabled = false;
                return;
            }
            this.providerCode = providerCode;
            this.gridControlEdit.DataSource = new List<InvoiceBill>();

            var service = EntityServiceFactory.GetEntityService<InvoiceBill, InvoiceBillService>();
            string exclude = "('**********' ";
            foreach (var existInvioceBillNo in existInvioceBillNos)
            {
                exclude += (String.Format(",'{0}'", existInvioceBillNo));
            }
            exclude += ",'**********') ";

            string condition = String.Format(ConditionInStoreBill, providerCode) +
                               String.Format(" and InvoiceNo not in {0}", exclude);

            this.gridControlEdit.DataSource = service.GetEntityList(String.Format(condition, providerCode));

            this.gridView2.CellValueChanged += gridView2_CellValueChanged;
          
        }

        public List<InvoiceBill> FInvoiceBill { get; private set; }

        void gridView2_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.FieldName == "PayAmountTax")
            {
                var invoiceBill = (InvoiceBill)this.gridView2.GetRow(e.RowHandle);
                if (e.Value != null)
                {
                    decimal d = 0;
                    if (Decimal.TryParse(e.Value.ToString(), out d))
                    {
                        if(d<0)
                        {
                            XtraMessageBox.Show("分摊金额不能小于0！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            gridView2.SetRowCellValue(e.RowHandle, e.Column, 0);
                        }
                        if (d + invoiceBill.InStoreBillAmount > invoiceBill.AmountTax)
                        {
                            XtraMessageBox.Show("分摊金额不能超过剩余金额！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            gridView2.SetRowCellValue(e.RowHandle,e.Column,0);
                        }
                    }
                }
            }
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            var list = (List<InvoiceBill>)this.gridControlEdit.DataSource;
            foreach (var extendInvoiceBill in list)
            {
                if (extendInvoiceBill.PayAmountTax >0)
                {
                    FInvoiceBill.Add(extendInvoiceBill);
                }
            }
            if (FInvoiceBill.Count > 0)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                XtraMessageBox.Show("请输入发票分摊金额！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void CmdCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}